<template>
  <van-popup v-model:show="isShow" class="share">
    <img src="https://static.hd.xxx.com/jye/fe-ssr/images/share@2x.png" @click="hide">
  </van-popup>
</template>
<script>
import { wbBase } from '@/components/utils/wbBase'
import { isWeiXin, isWeibo, getQueryString } from '@/plugins/utils'
import { commonUtils } from '@/components/utils/common-utils'
import { ref } from 'vue'
import { mapState } from 'vuex'

export default {
  mixins: [wbBase, commonUtils],
  setup () {
    return {
      isShow: ref(false),
    }
  },
  created () {
    if (!this.wxTicket) {
      this.post("/feinit/share", { url: encodeURIComponent(window.location.href) });
    }
  },
  computed: {
    ...mapState({
      wxTicket: state => state.siteInfo.wxTicket,
    }),
  },
  mounted () {
    if (!this.isH5Edit) {
      this.hide();
      this.initShareData()
    }
  },
  methods: {
    show() {
      this.isShow = true
    },
    hide() {
      this.isShow = false
    },
    initShareData() {
      const siteInfo = this.siteInfo
      if(this.isH5Edit || !this.siteAttrs.share || !this.wxTicket){
        return
      }
      let shareData = this.siteAttrs.share
      let link = window.location.href.split("?")[0] + "?id=" + getQueryString("id")
      if(this.userInfo.uid){
        link += "&uid=" + this.userInfo.uid
      }
      let wxShareData = {
        desc: shareData.bIsWxChecked ? shareData.wxShare.desc : shareData.allShare.desc,
        title: shareData.bIsWxChecked ? shareData.wxShare.title : shareData.allShare.title,
        link: link,
        imgUrl: shareData.icon,
        trigger: function(res) {
          console.log(res);
          // wxShareData.title = ""
        },
        success: function(res) {
          console.log(res);
        }
      };

      let wbShareData = {
        icon: shareData.icon,
        desc: shareData.bIsWbChecked ? shareData.wbShare.title : shareData.allShare.title,
        title: shareData.bIsWbChecked ? shareData.wbShare.desc : shareData.allShare.desc,
        link: link,
      };

      let signPackage = this.wxTicket;
      let share = {
        wxInit: function() {
          window.wx.config({
            debug: false,
            appId: signPackage["appId"],
            timestamp: signPackage["timestamp"],
            nonceStr: signPackage["nonceStr"],
            signature: signPackage["signature"],
            jsApiList: [
              // 所有要调用的 API 都要加到这个列表中
              "onMenuShareTimeline",
              "onMenuShareAppMessage",
            ]
          });
          window.wx.onMenuShareTimeline(wxShareData);
          window.wx.onMenuShareAppMessage(wxShareData);
        },
        wbInit: function() {
          window.WeiboJSBridge.invoke(
            "setSharingContent",
            {
              external: wbShareData
            },
            {
              success: function(params) {
                console.log("success_" + JSON.stringify(params));
              },
              fail: function(params, code) {
                console.log("fail_" + JSON.stringify(params) + "_" + code);
              },
              final: function(params) {
                console.log("final" + JSON.stringify(params));
              }
            }
          );
          
          window.WeiboJSBridge.invoke('getBrowserInfo', {}, {
            success: function(params) {
              siteInfo.wbFrom = params.from
            },
            fail: function(params, code) {
              this.showAlert('获取浏览器信息失败！' + code);
            },
            final: function(params) {
              // alert('定位结束！');
            },
          })
          window.WeiboJSBridge.invoke('getAid', {}, {
            success: function(params) {
              siteInfo.wbAid = params.aid
            },
            fail: function(params, code) {
              this.showAlert('获取浏览器信息失败！' + code);
            },
            final: function(params) {
              // alert('定位结束！');
            },
          })
        }
      };
      if (isWeiXin()) {
        if(window.wx){
          share.wxInit();
        }else{
          this.dynamicsLoadScript(this.env.baseUrl + 'js/jweixin-1.6.0.js', 'wx', function () {
            // window.wx.ready(function() {
              share.wxInit()
            // })
          })
        }
      }else if(isWeibo()){
        if (window.WeiboJSBridge) {
          share.wbInit();
        } else {
          document.addEventListener("WeiboJSBridgeReady", function() {
            share.wbInit();
          });
        }
      }
      // function setshare() {
      //   if (pattern_wx.test(userAgent)) {
      //     share.wxInit();
      //   }
      //   if (pattern_weibo.test(userAgent)) {
      //     share.wbInit();
      //   }
      // }
    }
  },
  watch: {
    wxTicket() {
      this.initShareData();
    }
  }
};
</script>
<style scoped>
.van-popup{
  background-color: transparent;
}
</style>

